package com.tb.controller;

import com.tb.dubbo.ISolrService;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import com.tb.utils.ReturnList;
import com.tb.utils.Utf8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author 何龙
 * @create 2019-01-10 21:58
 */
@RestController
@RequestMapping("/solr")
public class SolrController {

    /**
     * @api {GET} /solr/add   商品信息加入solr
     * @apiDescription 将数据库所有商品信息加入solr服务器
     * <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup Solr API
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *     {
     *     "code": 0,
     *     "msg": "",
     *     "data": "商品信息录入完毕!"
     *     }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "内部服务器发生错误!",
     *     "data": null
     *     }
     *
     * @apiSampleRequest  /solr/add
     */
    @Autowired
    private ISolrService iSolrService;
    @GetMapping("/add")
    public Msg add(){
        if (iSolrService.addSolr()){
            return Msg.success("商品信息录入完毕!");
        }else {
            return Msg.error(1,"内部服务器发生错误！");
        }
    }

/**
 * @api {get} /solr/query  前台全局搜索商品列表
 * @apiDescription 前台全局搜索商品列表
 *       <br/><span style="color:#012456"><b>作者:何龙</b></span>
 * @apiGroup Solr API
 *
 * @apiParam {String} [query] 查询关键字
 * @apiParam {int} [page=1] 页码数
 * @apiParam {int} [rows=10] 每页数据条数
 * @apiParam {String="任意非空字符串,按照销量降序"} [goodsSold] 销量排序
 * @apiParam {String="desc","asc"} [goodsPrice] 销量排序,desc降序,asc升序
 *
 * @apiParamExample {json} 请求参数格式:
 *      {
 *          "query": "手机",
 *          "page": 1,
 *          "rows": 1,
 *          "goodsSold":"xx",
 *          "goodsPrice":"desc"
 *      }
 *
 * @apiVersion 1.0.0
 *
 * @apiSuccess  {int} code 返回状态码 <br/> <span style="color:#1AA034">0:正常 </span>
 *                                    <br/> <span style="color:red">1:参数错误</span>
 * @apiSuccess  {string} msg 返回错误信息
 * @apiSuccess  {Object} data 返回数据结果集
 * @apiSuccess  (data) {int} total 数据总条数
 * @apiSuccess  (data) {list} list 返回数据结果集
 * @apiSuccess  (list) {string} goodsId 商品id
 * @apiSuccess  (list) {string} goodsName 商品简介
 * @apiSuccess  (list) {float} goodsPrice 商品价格
 * @apiSuccess  (list) {int} goodsSold 商品销量
 * @apiSuccess  (list) {int} goodsComments 商品评论数
 * @apiSuccess  (list) {string} img 图片链接
 *
 * @apiSuccessExample {json} 正确返回值:
 *{
 *     "code": 0,
 *     "msg": "",
 *     "data": {
 *         "total": 3,
 *         "list": [
 *             {
 *                 "goodsId": "7",
 *                 "goodsName": "小米Mix3 6GB+128GB黑色 骁龙845 全网通4G 双卡双待 全面屏拍照游戏智能手机",
 *                 "goodsPrice": 3299,
 *                 "goodsNum": null,
 *                 "goodsKind": null,
 *                 "goodsWeight": null,
 *                 "goodsSold": 6516,
 *                 "goodsComments": 66,
 *                 "goodsType": null,
 *                 "goodsDiscount": null,
 *                 "img": "//img14.360buyimg.com//n0/jfs/t1/708/33/12050/20042/5bd072f9E6702c378/3d2b5d137aea371a.jpg",
 *                 "imgs": [],
 *                 "orderField": "goods_id",
 *                 "orderType": "desc",
 *                 "listComments": null
 *             },
 *             {
 *                 "goodsId": "6",
 *                 "goodsName": "荣耀畅玩8C两天一充 莱茵护眼 刘海屏 全网通版4GB+32GB 幻夜黑 移动联通电信4G全面屏手机 双卡双待",
 *                 "goodsPrice": 899.1,
 *                 "goodsNum": null,
 *                 "goodsKind": null,
 *                 "goodsWeight": null,
 *                 "goodsSold": 3254,
 *                 "goodsComments": 213,
 *                 "goodsType": null,
 *                 "goodsDiscount": null,
 *                 "img": "https://img14.360buyimg.com/n0/jfs/t1/17309/16/2136/231494/5c19b7bfEc5b297bc/ae0039465606955d.jpg",
 *                 "imgs": [],
 *                 "orderField": "goods_id",
 *                 "orderType": "desc",
 *                 "listComments": null
 *             }
 *         ]
 *     }
 * }
 *
 * @apiErrorExample {json} 错误返回值:
 *     {
 *     "code": 2,
 *     "msg": "goodsPrice参数不合法！",
 *     "data": null
 *     }
 *
 * @apiSampleRequest /solr/query
 */


    @GetMapping("/query")
    public Msg query(
            @RequestParam(value = "query" ,defaultValue = "")String query,
            @RequestParam(value = "page" ,defaultValue = "1")Integer page,
            @RequestParam(value = "rows" ,defaultValue = "10")Integer rows,
            @RequestParam(value = "goodsSold" ,defaultValue = "")String goodsSold,
            @RequestParam(value = "goodsPrice" ,defaultValue = "")String goodsPrice
    ){
        page=page<1?1:page;

        if (!"".equals(goodsPrice)){
            if (!("asc".equals( goodsPrice )||"desc".equals( goodsPrice ))){
                return Msg.error( 2,"goodsPrice参数不合法！" );
            }
        }
        ReturnList solr = iSolrService.getSolr( Utf8.getString(query), page, rows, goodsSold ,goodsPrice);
        return Msg.success(solr);
    }


    /**
     * @api {get} /solr/adminQuery 后台商品列表
     * @apiDescription 后台商品列表&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要在管理员登陆状态下访问]</i></span>
     *                  <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup Solr API
     *
     * @apiParam {String} [query] 查询关键字
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=5] 每页数据条数
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "query": "手机",
     *          "page": 1,
     *          "rows": 2
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">999:未登录或者登陆超时</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {string} goodsId 商品id
     * @apiSuccess  (list) {string} goodsName 商品简介
     * @apiSuccess  (list) {float} goodsPrice 商品价格
     * @apiSuccess  (list) {int} goodsSold 商品销量
     * @apiSuccess  (list) {int} goodsComments 商品评论数
     * @apiSuccess  (list) {string} img 图片链接
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 7,
     *         "list": [
     *             {
     *                 "goodsId": "1",
     *                 "goodsName": "iPhone10",
     *                 "goodsPrice": 4000,
     *                 "goodsNum": null,
     *                 "goodsKind": null,
     *                 "goodsWeight": null,
     *                 "goodsSold": 1000,
     *                 "goodsComments": 1,
     *                 "goodsType": null,
     *                 "goodsDiscount": null,
     *                 "img": "/bbb",
     *                 "imgs": [],
     *                 "orderField": "goods_id",
     *                 "orderType": "desc",
     *                 "listComments": null
     *             },
     *             {
     *                 "goodsId": "154726958819188",
     *                 "goodsName": "苹果66s",
     *                 "goodsPrice": null,
     *                 "goodsNum": null,
     *                 "goodsKind": null,
     *                 "goodsWeight": null,
     *                 "goodsSold": null,
     *                 "goodsComments": null,
     *                 "goodsType": null,
     *                 "goodsDiscount": null,
     *                 "img": "ssssssfdgfgfdg",
     *                 "imgs": [],
     *                 "orderField": "goods_id",
     *                 "orderType": "desc",
     *                 "listComments": null
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /solr/query
     */

    @GetMapping("/adminQuery")
    public Msg adminQuery(
            @RequestParam(value = "query" ,defaultValue = "")String query,
            @RequestParam(value = "page" ,defaultValue = "1")Integer page,
            @RequestParam(value = "rows" ,defaultValue = "5")Integer rows,
            HttpSession httpSession
    ){

        try {
            //验证是否登录
            User user = (User) httpSession.getAttribute( "AdminUser" );
            user.getId();
            page= page<1?1:page;
            ReturnList solr = iSolrService.getSolr( Utf8.getString(query), page, rows, "" ,"");
            return Msg.success(solr);
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }

    }

}
